//
//  MessageTimeStatusCell.swift
//  MT
//
//  Created by ooops on 2019/12/15.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MessageTimeStatusCell: MessageTableViewBaseCell {

    override class func reuseIdentity() -> String {
        return "MessageTimeStatusCellID"
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setupUI() {
        super.setupUI()
        contentView.addSubview(timeStatusLabel)
        
        contentView.addSubview(cycleView)
    }
    
    override func bindData(_ info: JCVerificationInfo) {
        super.bindData(info)
        timeStatusLabel.text = updateTimeToCurrennTime(timeStamp: info.timestamp)
    }
    
    override func makeConstraint() {
        super.makeConstraint()
        
        cycleView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(timeStatusLabel.snp.left).offset(-7)
            make.width.height.equalTo(8)
        }
        
        timeStatusLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
    }
    
    lazy var cycleView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0xFFC000)
        v.layer.cornerRadius = 4
        return v
    }()
    
    lazy var timeStatusLabel: UILabel = {
        let lbl: UILabel = UILabel()
        lbl.textColor = xF3F3F3Color
        lbl.font = PingFangSCRegularFont(12)
        lbl.numberOfLines = 1
        lbl.sizeToFit()
        return lbl
    }()
    
    func updateTimeToCurrennTime(timeStamp: Double) -> String {
        //获取当前的时间戳
        cycleView.isHidden = true
        let currentTime = Date().timeIntervalSince1970
        debugPrint(currentTime,   timeStamp, "sdsss")
        //时间差
        let reduceTime : TimeInterval = currentTime - timeStamp
        //时间差小于60秒
        if reduceTime < 60 {
            cycleView.isHidden = false
            return "刚刚"
        }
        //时间差大于一分钟小于60分钟内
        let mins = Int(reduceTime / 60)
        if mins < 60 {
            return "\(mins)分钟前"
        }
        let hours = Int(reduceTime / 3600)
        if hours < 24 {
            return "\(hours)小时前"
        }
        let days = Int(reduceTime / 3600 / 24)
        if days < 30 {
            return "\(days)天前"
        }
        //不满足上述条件---或者是未来日期-----直接返回日期
        let date = NSDate(timeIntervalSince1970: timeStamp)
        let dfmatter = DateFormatter()
        //yyyy-MM-dd HH:mm:ss
        dfmatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
        return dfmatter.string(from: date as Date)
    }
    
}
